﻿package cn.lite3.notepaper 
{
	import cn.lite3.notepaper.managers.NotepaperWindowManager;
	import flash.display.StageDisplayState;
	import flash.events.FullScreenEvent;
	import flash.events.MouseEvent;
	import flash.events.ScreenMouseEvent;
	
	import flash.desktop.NativeApplication;
	import flash.desktop.SystemTrayIcon;
	import flash.display.NativeMenu;
	import flash.display.NativeMenuItem;
	import flash.events.Event;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	/**
	 * ...
	 * @author lite3
	 */
	public class TrayIcon
	{
		
		[Embed(source='../../../assets/trayIcon.png')]
		private const SystemIconBitmap:Class;
		
		private var notepaperWindowManager:NotepaperWindowManager;
		
		public function TrayIcon(trayIcon:SystemTrayIcon) 
		{
			notepaperWindowManager = NotepaperWindowManager.getInstance();
			
			trayIcon.bitmaps = [new SystemIconBitmap()];
			trayIcon.menu = getTrayMenu();
			trayIcon.addEventListener(MouseEvent.CLICK, showAllNotepaperWindow);
		}
		
		private function showAllNotepaperWindow(e:MouseEvent):void 
		{
			if (notepaperWindowManager.getNotepaperWindowCount() > 0)
			{
				if (notepaperWindowManager.getNotepaperWindowShowCount() > 0)
				{
					notepaperWindowManager.hideAll();
				}else
				{
					notepaperWindowManager.showAll();
				}
			}
			
		}
		
		private function getTrayMenu():NativeMenu
		{
			var newNotepaperItem:NativeMenuItem = new NativeMenuItem("新建便签");
			newNotepaperItem.addEventListener(Event.SELECT, function(e:Event):void
			{
				notepaperWindowManager.createWindow();
			});
			
			var allDisplayStateItem:NativeMenuItem = new NativeMenuItem("显示所有");
			allDisplayStateItem.addEventListener(Event.SELECT, function(e:Event):void
			{
				if (allDisplayStateItem.label == "显示所有")
				{
					notepaperWindowManager.showAll();
				}else
				{
					notepaperWindowManager.hideAll();
				}
			});
			allDisplayStateItem.addEventListener(Event.DISPLAYING, function(e:Event):void
			{
				var allDisplayStateItemLabel:String = "隐藏所有";
				if (notepaperWindowManager.getNotepaperWindowCount() > notepaperWindowManager.getNotepaperWindowShowCount())
				{
					allDisplayStateItemLabel = "显示所有";
				}
				
				allDisplayStateItem.enabled = notepaperWindowManager.getNotepaperWindowCount() != 0;
				allDisplayStateItem.label = allDisplayStateItemLabel;
			});
			
			var alwaysInFrontItem:NativeMenuItem = new NativeMenuItem("总在最前端");
			alwaysInFrontItem.checked = NotepaperWindowManager.getInstance().alwaysInFrontAll;
			alwaysInFrontItem.addEventListener(Event.SELECT, function(e:Event):void
			{
				alwaysInFrontItem.checked = !alwaysInFrontItem.checked;
				NotepaperWindowManager.getInstance().alwaysInFrontAll = alwaysInFrontItem.checked;
			});
			
			var startAtLoginItem:NativeMenuItem = new NativeMenuItem("开机自启动");
			
			startAtLoginItem.addEventListener(Event.SELECT, function(e:Event):void
			{
				startAtLoginItem.checked = !startAtLoginItem.checked;
				NativeApplication.nativeApplication.startAtLogin = startAtLoginItem.checked;
			});
			startAtLoginItem.addEventListener(Event.DISPLAYING, function(e:Event):void
			{
				startAtLoginItem.checked = NativeApplication.nativeApplication.startAtLogin;
			});
			
			var separator1Item:NativeMenuItem = new NativeMenuItem("separator1", true);
			var webItem:NativeMenuItem = new NativeMenuItem("www.lite3.cn");
			webItem.addEventListener(Event.SELECT, function (e:Event):void
			{
				navigateToURL(new URLRequest("http://www.lite3.cn"), "_blank");
			});
			
			var versionItem:NativeMenuItem = new NativeMenuItem(getVersion());
			versionItem.enabled = false;
			var separator2Item:NativeMenuItem = new NativeMenuItem("separator2", true);
			var exitItem:NativeMenuItem = new NativeMenuItem("退出");
			exitItem.addEventListener(Event.SELECT, function (e:Event):void
			{
				NativeApplication.nativeApplication.dispatchEvent(new Event(Event.EXITING));
				NativeApplication.nativeApplication.exit();
			});
			
			var menu:NativeMenu = new NativeMenu();
			menu.addItem(newNotepaperItem);
			menu.addItem(allDisplayStateItem);
			menu.addItem(alwaysInFrontItem);
			menu.addItem(startAtLoginItem);
			menu.addItem(separator1Item);
			menu.addItem(webItem);
			menu.addItem(versionItem);
			menu.addItem(separator2Item);
			menu.addItem(exitItem);
			return menu;
		}
		
		private function getVersion():String
		{
			var appDescriptor:XML = NativeApplication.nativeApplication.applicationDescriptor;
			var ns:Namespace = appDescriptor.namespace();
			var appCopyright:String = appDescriptor.ns::copyright;
			var appVersion:String = appDescriptor.ns::version;
			
			return appVersion;
		}
	}
}